package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {

    private static String SECRET_KEY = "11111111111111111111111111111111";

    private static Key KEY = Keys.hmacShaKeyFor(SECRET_KEY.getBytes());


    private static Long EXPIRE = 1000 * 60 * 60 * 12L;

    /**
     * 创建JWT
     * @param claimsMap 用户信息
     * @return 返回一个
     */
    public static String create(Map<String,Object> claimsMap){
        /*签发时间*/
        Date now = new Date();

        return Jwts.builder()
                .signWith(KEY)   //签发秘钥
                .setIssuedAt(now) // 执行时间
                .setExpiration(new Date(now.getTime() + EXPIRE)) //过期时间
                .setClaims(claimsMap)
                .compact();
    }

    public static Map<String,Object> parse(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
